$(document).ready(function(){
	
	loadZoneStart();
	loadZoneEnd();
	loadStatus();
	
	$("select[name=zone_start]#inputZoneStart").change(function() {
		loadRouteStart();
	});
	
	$("select[name=zone_end]#inputZoneEnd").change(function() {
		loadRouteEnd();
	});
});

function loadZoneStart() {
	$.ajax({
		url : 'CS24001Srvl' , 
		type : "POST",
		data : {
			process_type: "getZone"
		},
	//	anync:false,
		dataType:"JSON",
		success: function(data){
			var selected="";
			var options = '';
			options += '<option value="">-- เลือกเขต --</option>';
			for (var i = 0; i < data.values.length; i++) {
				options += '<option value="' + data.values[i] + '" '+selected+'>' + data.messages[i] +  '</option>';
			}
			$("select[name=zone_start]#inputZoneStart").html(options);
		}
	});
}

function loadZoneEnd() {
	$.ajax({
		url : 'CS24001Srvl' , 
		type : "POST",
		data : {
			process_type: "getZone" 
		},
	//	anync:false,
		dataType:"JSON",
		success: function(data){
			var selected="";
			var options = '';
			options += '<option value="">-- เลือกเขต --</option>';
			for (var i = 0; i < data.values.length; i++) {
				options += '<option value="' + data.values[i] + '" '+selected+'>' + data.messages[i] +  '</option>';
			}
			$("select[name=zone_end]#inputZoneEnd").html(options);
		}
	});
}

function loadRouteStart(zoneCode) {
	var vaZoneCode = $( "#inputZoneStart :selected").val();
	if(zoneCode!=""){
		vaZoneCode = zoneCode;
	}
	if(isNaN(vaZoneCode)==true){
		vaZoneCode = document.getElementById("inputZoneStart").value;
	}
	$.ajax({
		url : 'CS24001Srvl' , 
		type : "POST",
		data : {
			process_type: "getRoute" , 
			zoneCode: vaZoneCode
		},
	//	anync:false,
		dataType:"JSON",
		success: function(data){
			var options = '';
			options += '<option value="">-- เลือกเส้นทาง --</option>';
			var selected ="";
			for (var i = 0; i < data.values.length; i++) {
				options += '<option value="' + data.values[i] + '" '+selected+'>' + data.messages[i] +  '</option>';
			}
			$("select#inputRouteStart").html(options);
		}
	});
}

function loadRouteEnd(zoneCode) {
	var vaZoneCode = $( "#inputZoneEnd :selected").val();
	if(zoneCode!=""){
		vaZoneCode = zoneCode;
	}
	if(isNaN(vaZoneCode)==true){
		vaZoneCode = document.getElementById("inputZoneEnd").value;
	}
	$.ajax({
		url : 'CS24001Srvl' , 
		type : "POST",
		data : {
			process_type: "getRoute" , 
			zoneCode: vaZoneCode
		},
	//	anync:false,
		dataType:"JSON",
		success: function(data){
			var options = '';
			options += '<option value="">-- เลือกเส้นทาง --</option>';
			var selected ="";
			for (var i = 0; i < data.values.length; i++) {
				options += '<option value="' + data.values[i] + '" '+selected+'>' + data.messages[i] +  '</option>';
			}
			$("select#inputRouteEnd").html(options);
		}
	});
}

function loadStatus() {
	$.ajax({
		url : 'CS24001Srvl' , 
		type : "POST",
		data : {
			process_type: "getStatus" 
		},
	//	anync:false,
		dataType:"JSON",
		success: function(data){
			var selected="";
			var options = '';
			options += '<option value="">-- เลือกสถานะผู้ใช้น้ำ --</option>';
			for (var i = 0; i < data.values.length; i++) {
				options += '<option value="' + data.values[i] + '" '+selected+'>' + data.messages[i] +  '</option>';
			}
			$("select[name=status]#inputStatus").html(options);
		}
	});
}

function printReport() {
	var vaZoneStart = $("#inputZoneStart").val();
	var vaZoneEnd = $("#inputZoneEnd").val();
	var vaRouteStart = $("#inputRouteStart").val();
	var vaRouteEnd = $("#inputRouteEnd").val();
	var vaDateStart = $("#datetimepicker1").val();
	var vaDateEnd = $("#datetimepicker2").val();
	var vaStatus = $("#inputStatus").val();
	var vnUserId = $("#userId").val();
	var vnSiteId = $("#siteId").val();
	
	if (vaZoneStart.trim().length == 0 || vaZoneEnd.trim().length == 0) {
//			|| vaRouteStart.trim().length == 0
//		|| vaRouteEnd.trim().length == 0 || vaDateStart.trim().length == 0 || vaDateEnd.trim().length == 0
//		|| vaStatus.trim().length == 0 || vnUserId.trim().length == 0 || vnSiteId.trim().length == 0) {
		$("#modal_content").html("กรุณากรอกข้อมูลให้ครบถ้วน");
		$("#warningModal").modal();
		return;
	} else {

		var url = reporturl+"CS24001R.jsp?siteId="+vnSiteId+"&userId="+vnUserId+"&routeFrom="+vaRouteStart+"&routeTo"+vaRouteEnd+"&fromDate="+vaDateStart+"&toDate="+vaDateEnd+"&zoneFrom="+vaZoneStart+"&zoneTo="+vaZoneEnd+"&statusC="+vaStatus;
		window.open(url);
	}
	
}
